#define PrynInternal
#define PrynImplementation
#include "platform.h"

PrynResult prynStatePlatformAllocate (PrynState *state, uintptr_t length, void **output)
{
	PrynStateWindows *platform;

	if (!state || !state->pPlatform) return PrynResultNullArgument;
	platform = (PrynStateWindows *) state->pPlatform;

	if (!length) return PrynResultSuccess;
	if ((*output = HeapAlloc (platform->heap, 0, length)) == 0)
		return PrynResultOutOfMemory;
	return PrynResultSuccess;
}

PrynResult prynStatePlatformFree (PrynState *state, void *pointer)
{
	PrynStateWindows *platform;

	if (!state || !state->pPlatform || !pointer) return PrynResultNullArgument;
	platform = (PrynStateWindows *) state->pPlatform;
	
	HeapFree (platform->heap, 0, pointer);
	return PrynResultSuccess;
}

PrynResult prynStatePlatformReallocate (PrynState *state, void **pointer, uintptr_t length)
{
	PrynStateWindows *platform;
	void *result = 0;

	if (!state || !state->pPlatform || !pointer) return PrynResultNullArgument;
	platform = (PrynStateWindows *) state->pPlatform;
	
	if (!length)
	{
		if (*pointer)
			HeapFree (platform->heap, 0, *pointer);
		*pointer = 0;
		return PrynResultSuccess;
	}
	
	if (*pointer)
		result = HeapReAlloc (platform->heap, 0, *pointer, length);
	else
		result = HeapAlloc (platform->heap, 0, length);
		
	if (!result) return PrynResultOutOfMemory;
	*pointer = result;
	return PrynResultSuccess;
}
